package com.lam.framework.security.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.lam.common.base.domain.LoginUser;
import com.lam.common.exception.BusinessException;

/**
 * 用户验证处理
 *
 * @author lam
 */
//@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	
	private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

	@Autowired
	private UserService userService;

	@Override 
	public UserDetails loadUserByUsername(String username) {

		LoginUser loginUser = userService.loadUserByUsername(username);

		if (loginUser == null) {
			log.error("登录用户账号：{} 不存在.", username);
			throw new UsernameNotFoundException("用户名或密码错误！");
		}
		
		if (!loginUser.isEnabled()) {
			log.error("登录用户账号：{} 已被停用.", username);
			throw new BusinessException("您的账号【" + username + "】已停用，请联系管理员！");
		}
		
		return loginUser;
	}

}
